﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Things.Common;

namespace Things.ThingsWeb.Components
{
    public partial class Register : System.Web.UI.UserControl
    {
        protected string SourceURL
        {
            get
            {
                string temp = "../default.aspx";
                if (Session[ConstantString.SessionSourceURL] != null && Session[ConstantString.SessionSourceURL].ToString().Trim() != "")
                {
                    temp = Session[ConstantString.SessionSourceURL].ToString();
                }


                return temp;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.txtAccount.Text.Trim().Length == 0)
            {
                strErr += "帐号(邮箱或者手机号)不能为空！\\n";
                this.lblMsg.Text = strErr;
                return;
            }

            if (PageValidate.IsEmail(this.txtAccount.Text.Trim()) == false && PageValidate.IsPhone(this.txtAccount.Text.Trim()) == false)
            {
                strErr += "帐号(邮箱或者手机号)格式有误！\\n";
                this.lblMsg.Text = strErr;
                return;
            }
            if (this.txtPassword.Text.Trim().Length == 0)
            {
                strErr += "密码不能为空！\\n";
                this.lblMsg.Text = strErr;
                return;
            }
            if (this.txtPassword.Text.Trim().Length < 6 || this.txtPassword.Text.Trim().Length > 12)
            {
                strErr += "密码长度不能小于6或大于12！\\n";
                this.lblMsg.Text = strErr;
                return;
            }
            if (this.txtConfirmPassword.Text.Trim().Length == 0)
            {
                strErr += "密码不能为空！\\n";
                this.lblMsg.Text = strErr;
                return;
            }
            if (this.txtPassword.Text.Trim().ToLower() != this.txtConfirmPassword.Text.Trim().ToLower())
            {
                strErr += "密码与确认密码不一致！\\n";
                this.lblMsg.Text = strErr;
                return;
            }
            if ((Session[ConstantString.SessionCheckCode] != null) && (Session[ConstantString.SessionCheckCode].ToString() != ""))
            {
                if (Session[ConstantString.SessionCheckCode].ToString().ToLower() != this.CheckCode.Value.ToLower())
                {
                    strErr += "所填写的验证码与所给的不符 !";
                    this.lblMsg.Text = strErr;
                    Session[ConstantString.SessionCheckCode] = null;
                    return;
                }
                else
                {
                    Session[ConstantString.SessionCheckCode] = null;
                }
            }
            else
            {
                strErr += "验证码信息有误 !";
                this.lblMsg.Text = strErr;
                return;
            }

            if (strErr != "")
            {
                MessageBox.Show(this.Page, strErr);
                return;
            }
            string Account = Things.Common.PageValidate.InputText(this.txtAccount.Text.Trim(), 30);
            string Password = DESEncryptDecrypt.Encrypt(Things.Common.PageValidate.InputText(this.txtPassword.Text.Trim(), 30));
            bool IsDisabled = false;
            DateTime CreatedDate = DateTime.Now;

            Things.Model.Buyer model = new Things.Model.Buyer();
            model.Account = Account;
            model.Password = Password;
            model.IsDisabled = IsDisabled;
            model.CreatedDate = CreatedDate;

            Things.BLL.Buyer bll = new Things.BLL.Buyer();

            if (bll.Exists(Account) == true)
            {
                strErr += "该帐号已注册，请用其他帐号注册 !";
                this.lblMsg.Text = strErr;
                return;
            }

            if (bll.Add(model) == true)
            {
                Session[ConstantString.SessionCurrentBuyerAccount] = model;
                Things.Common.MessageBox.ShowAndRedirect(this.Page, "注册成功！", SourceURL);
            }
            else
            {
                MessageBox.Show(this.Page, "注册失败!");
            }

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect(SourceURL);
        }
    }
}